<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>下单</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        table, tr, th, td {
            border: 1px solid #5fbdcb;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div>
    您所要下单的商品为：
    <span id="span"></span><br>
    可用的优惠券：
    <table></table>
    <button id="button1">下单</button>
    <a href="">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    let dishId = getParam(url, 'dishId');
    let amount = 0;
    let value = 0;
    let couponId = 0;
    $(function () {
        let a = document.querySelector('a');
        a.href = '/User/UserDishPage.jsp?storeId=' + storeId;
        $.ajax({
            url: "http://localhost:8080/dish.do",
            type: "post",
            data: {
                storeId: storeId,
                dishId: dishId,
                method: 'selectOne'
            },
            dataType: "text",
            success: function (data) {
                let parse = JSON.parse(data);
                let span = document.querySelector('span');
                span.innerText = parse.dishName;
                amount = parse.price;
                $.ajax({
                    url: "http://localhost:8080/coupon.do",
                    type: "post",
                    data: {
                        amount: amount,
                        storeId: storeId,
                        method: 'getCoupon'
                    },
                    dataType: "text",
                    success: function (data) {
                        if (data === 'login') {
                            alert('请先登录!');
                            window.location.href = 'DishPage.jsp?storeId=' + storeId;
                        } else {
                            let parse = JSON.parse(data);
                            let table = document.querySelector('table');
                            if (parse.length === 0) {
                                let tr = document.createElement('tr');
                                tr.innerText = '无优惠券可用';
                                table.appendChild(tr);
                            } else {
                                for (let i = 0; i < parse.length; i++) {
                                    couponId = parse[i].couponId;
                                    let tr = document.createElement('tr');
                                    table.appendChild(tr);
                                    let th1 = document.createElement('th');
                                    th1.innerText = '满' + parse[i].min + '减' + parse[i].value;
                                    tr.appendChild(th1);
                                    let td1 = document.createElement('td');
                                    tr.appendChild(td1);
                                    let a = document.createElement('a');
                                    a.innerText = '用券下单';
                                    a.href = '/User/CouponOrder.jsp?storeId=' + storeId + '&couponId=' + parse[i].couponId + '&dishId=' + dishId + '&money=' + (amount - parse[i].value);
                                    td1.appendChild(a);
                                }
                            }
                        }
                    }
                })
            }
        })
        $('#button1').on('click', function () {
            $.ajax({
                url: "http://localhost:8080/order.do",
                type: "post",
                data: {
                    dishId: dishId,
                    storeId: storeId,
                    amount: amount,
                    method: 'orderOne'
                },
                dataType: "text",
                success: function (data) {
                    if (data === 'ok') {
                        alert('下单成功');
                        window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
                    }
                    if (data === 'error') {
                        alert('下单失败');
                    }
                    if (data === 'balance') {
                        alert('余额不足，请先充值！');
                        window.location.href = '/User/Recharge.jsp';
                    }
                    if (data === 'login') {
                        alert('请先登录!');
                        window.location.href = 'DishPage.jsp?storeId=' + storeId;
                    }
                }
            })
        })
    })
</script>
</body>
</html>
